package com.ruoyi.project.sales.orders.search.service;

import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.sales.orders.search.domain.*;
import com.ruoyi.project.sales.orders.search.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderCustomerMapper orderCustomerMapper;

    @Autowired
    private OrderProductMapper orderProductMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Orders> selectOrderList(Orders order) {

        return ordersMapper.selectOrders (order);

    }

    @Override
    public int insertOrder(Orders orders) {

        int i3 = orderProductMapper.insertProductOrder (orders.getProduct_id (),orders.getOrder_id ());

        int i4 = orderCustomerMapper.insertCustomerOrder (orders.getOrder_id (),orders.getCustomer_id ());

        Customer customer = new Customer (orders.getCustomer_id (),orders.getCustomer_name (),
                orders.getCustomer_relate (),orders.getCustomer_info (),
                orders.getCustomer_tel (),orders.getCustomer_type ());

        Product product = new Product (orders.getProduct_id (),orders.getProduct_name (),orders.getProduct_num (),
                orders.getProduct_sum ());

        int i1 = customerMapper.insertCustomer (customer);

        int i2 = productMapper.insertProduct (product);

        Orders o = new Orders ();
        o.setOrder_id (orders.getOrder_id ());
        o.setOrder_name (orders.getOrder_name ());
        o.setOrder_type (orders.getOrder_type ());
        o.setDeliver ("1");
        o.setOrder_info (orders.getOrder_info ());
        o.setContract (null);
        o.setCustomer_file (orders.getCustomer_file ());
        o.setSale_name (orders.getSale_name ());
        o.setManager_name (orders.getManager_name ());
        o.setOrder_date (orders.getOrder_date ());
        o.setProduct_sum (orders.getProduct_sum ());

        int i = ordersMapper.insertOrders (o);

        return i;
    }

    @Override
    public Orders selectOrdersById(Integer id) {
        return ordersMapper.selectOrdersById(id);
    }

    @Override
    public int updateOrders(Orders orders) {

        Integer order_id = orders.getOrder_id ();
        String customer_file = orders.getCustomer_file ();
        int i = customerMapper.updateCustomer (order_id, customer_file);

        Integer product_sum = orders.getProduct_sum ();

        int i1 = productMapper.updateProduct (order_id, product_sum);


        return ordersMapper.updateType(orders);
    }

    @Override
    public int deleteOrdersByIds(String ids) {
        Long[] postIds = Convert.toLongArray (ids);
        return ordersMapper.deleteByIds(postIds);
    }


}
